package com.idanchuang.component.consumer.config;

import java.util.*;

/**
 * @author yjy
 * @date 2019/11/21 9:06
 **/
public class RpcConfig {

    /** 签名key */
    private String signKey;
    /** 用于签名的密钥 */
    private String secretKey;
    /** 额外的签名秘钥集合 */
    private List<String> extraSecretKeys;
    /** 扩展请求头 */
    private Map<String, String> headers;
    /** 扩展Query参数 */
    private Map<String, Collection<String>> queries;
    /** 本地变量表数据在RPC请求中传递时的HeaderName */
    private String threadLocalHeaderName;
    /** 统一传递当前请求的header项 */
    private List<String> unifyTransmitHeaders;
    /** 统一传递当前请求的query项 */
    private List<String> unifyTransmitQueries;
    /** 额外传递当前请求的header项 */
    private List<String> transmitHeaders;
    /** 额外传递当前请求的query项 */
    private List<String> transmitQueries;
    /** 单个请求头的大小限制, 单位: byte, 默认 16384 (16kb) */
    private int headerLimit = 16384;

    {
        signKey = "ABM-RPC-SIGN";
        secretKey = "123456";

        extraSecretKeys = new ArrayList<>();

        threadLocalHeaderName = "ABM-RPC-THREAD-LOCAL";
        headers = new HashMap<>();
        queries = new HashMap<>();

        /** config in apollo application: common, namespace: springcloud */
        unifyTransmitHeaders = new ArrayList<>();
        unifyTransmitQueries = new ArrayList<>();

        transmitHeaders = new ArrayList<>();
        transmitQueries = new ArrayList<>();
    }

    public Set<String> getAllTransmitHeaders() {
        Set<String> allTransmitHeaders = new HashSet<>();
        if (this.unifyTransmitHeaders != null && !this.unifyTransmitHeaders.isEmpty()) {
            for (String h : this.unifyTransmitHeaders) {
                if (h != null && !"".equals(h)) {
                    allTransmitHeaders.add(h);
                }
            }
        }
        if (this.transmitHeaders != null && !this.transmitHeaders.isEmpty()) {
            for (String h : this.transmitHeaders) {
                if (h != null && !"".equals(h)) {
                    allTransmitHeaders.add(h);
                }
            }
        }
        return allTransmitHeaders;
    }

    public Set<String> getAllTransmitQueries() {
        Set<String> allTransmitQueries = new HashSet<>();
        if (this.unifyTransmitQueries != null && !this.unifyTransmitQueries.isEmpty()) {
            for (String q : this.unifyTransmitQueries) {
                if (q != null && !"".equals(q)) {
                    allTransmitQueries.add(q);
                }
            }
        }
        if (this.transmitQueries != null && !this.transmitQueries.isEmpty()) {
            for (String q : this.transmitQueries) {
                if (q != null && !"".equals(q)) {
                    allTransmitQueries.add(q);
                }
            }
        }
        return allTransmitQueries;
    }

    public String getSignKey() {
        return signKey;
    }

    public void setSignKey(String signKey) {
        this.signKey = signKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public String getThreadLocalHeaderName() {
        return threadLocalHeaderName;
    }

    public void setThreadLocalHeaderName(String threadLocalHeaderName) {
        this.threadLocalHeaderName = threadLocalHeaderName;
    }

    public Map<String, Collection<String>> getQueries() {
        return queries;
    }

    public void setQueries(Map<String, Collection<String>> queries) {
        this.queries = queries;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public List<String> getTransmitHeaders() {
        return transmitHeaders;
    }

    public void setTransmitHeaders(List<String> transmitHeaders) {
        this.transmitHeaders = transmitHeaders;
    }

    public List<String> getTransmitQueries() {
        return transmitQueries;
    }

    public List<String> getExtraSecretKeys() {
        return extraSecretKeys;
    }

    public void setExtraSecretKeys(List<String> extraSecretKeys) {
        this.extraSecretKeys = extraSecretKeys;
    }

    public void setTransmitQueries(List<String> transmitQueries) {
        this.transmitQueries = transmitQueries;
    }

    public List<String> getUnifyTransmitHeaders() {
        return unifyTransmitHeaders;
    }

    public void setUnifyTransmitHeaders(List<String> unifyTransmitHeaders) {
        this.unifyTransmitHeaders = unifyTransmitHeaders;
    }

    public List<String> getUnifyTransmitQueries() {
        return unifyTransmitQueries;
    }

    public void setUnifyTransmitQueries(List<String> unifyTransmitQueries) {
        this.unifyTransmitQueries = unifyTransmitQueries;
    }

    public int getHeaderLimit() {
        return headerLimit;
    }

    public void setHeaderLimit(int headerLimit) {
        this.headerLimit = headerLimit;
    }

    @Override
    public String toString() {
        return "RpcConfig{" +
                "signKey='" + signKey + '\'' +
                ", secretKey='" + secretKey + '\'' +
                ", extraSecretKeys=" + extraSecretKeys +
                ", headers=" + headers +
                ", queries=" + queries +
                ", threadLocalHeaderName='" + threadLocalHeaderName + '\'' +
                ", unifyTransmitHeaders=" + unifyTransmitHeaders +
                ", unifyTransmitQueries=" + unifyTransmitQueries +
                ", transmitHeaders=" + transmitHeaders +
                ", transmitQueries=" + transmitQueries +
                ", headerLimit=" + headerLimit +
                '}';
    }

}
